﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;
using MVC_Project.Bll.Systems;
using RouteAttribute = System.Web.Http.RouteAttribute;

namespace MVC_Project.Api.Areas.Systems
{
    [ApiAuthorize]
    public class UsersController : ApiController
    {
        UsersBll bll = new UsersBll();

        [Route("api/Users/info")]
        public Models.Systems.Users Get()
        {

            //获取JWT 中验证后保存的数据
            var auth = Request.GetRouteData().Values["auth"] as AuthInfo;
            var data = bll.GetUsersById(auth.UserID);

            Models.Systems.Users user= new Models.Systems.Users();
            user.userID = data.userID;
            user.name = data.name;
            user.path = data.Images.path;
            user.phone = data.phone;
            user.status = data.status;
            user.loginName = data.loginName;
            user.sex = data.sex;
            user.roleName = data.Roles.roleName;

            return user;
        }

        /// <summary>
        /// 获取会员列表信息
        /// </summary>
        /// <returns></returns>
        public PageList<Models.Systems.Users> Get(int page, int size, string name = "")
        {
            
            PageList<Models.Systems.Users> re = new PageList<Models.Systems.Users>();

            int count = 0;
            var users = bll.GetUsersPage(page, size, name, out count);

            // 将数据模型 转换成 dto 模型
            List<Models.Systems.Users> list = new List<Models.Systems.Users>();
            foreach (var u in users)
            {
                list.Add(new Models.Systems.Users()
                {
                    userID = u.userID,
                    name = u.name,
                    imgID = u.imgID,
                    path = u.Images.path,
                    lastTime = u.lastTime,
                    loginName = u.loginName,
                    sex = u.sex,
                    phone = u.phone,
                    status = u.status,
                    loginPwd = u.loginPwd,
                    roleID = u.roleID,
                    roleName = u.Roles.roleName,

                });
            }

            re.code = 0;
            re.msg = "ok";
            re.data = list;
            re.count = count;
            re.page = page;
            re.size = size;

            return re;

        }
    
        // GET: api/Users/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/Users
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/Users/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/Users/5
        public void Delete(int id)
        {
        }
    }
}
